<?php
/* explain:		news system
 * Project:     
 * File:        news.php
 *
 * @ link 		http://www.../modules/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2005  Ling Deming
 * @ author 	ivan.ling  Ling Deming
 * @ version 	1.0
 *
 */
include_once("Common.php");
include_once("domain/KeywordDomain.php");
include_once("dao/CommonDAO.php");
include_once("dao/KeywordDAO.php");

class KeywordImpl extends Common implements KeywordDAO, CommonDAO
{
	private $conn;
	private $message;
	

	function KeywordImpl($DBTag)
	{
		parent::Common();
		
		$this->conn = $this->getConnection($DBTag);
		
	}

	/* get data via ID
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function getById($Id)
	{
		$sql  = 'SELECT white, black ';
		$sql .= "FROM keywords_coupon WHERE id = $Id";
		
		try {
			$rs = mysql_query($sql, $this->conn);
			if ($row = mysql_fetch_row($rs)) {
				$oKeywordDomain = new KeywordDomain();

				$oKeywordDomain->setCouponId($couponId);
				
				$oKeywordDomain->setWhite($row[0]);
				$oKeywordDomain->setBlack($row[1]);
				
				return $oKeywordDomain;
			} else {
				$this->messages = "get coupon data failed by Id. sql : $sql <br>";
				$this->messages .= "error: " . mysql_error($this->conn);

				return null;
			}
		} catch (Exception $e) {
			$this->messages = $e->getMessage();

			return null;
		}
	}

	public function add($oKeywordDomain)
	{
		if($oKeywordDomain instanceof KeywordDomain){
			
			$sql 	 = "INSERT INTO vote SET white = ";
			$sql	.= $oKeywordDomain->getWhite();
			
			$sql 	.= ", black = ";
			$sql	.= $oKeywordDomain->getBlack();
	
			$sql 	.= ", couponId = ";
			$sql	.= $oKeywordDomain->getCouponId();
			
			$id		= 0;
			
			try{
				$rs  	= mysql_query($sql, $this->conn);
				if($rs){
					$id = mysql_insert_id($this->conn);
					
					$oAttribute->setId($id);
					
					$this->messages = "add successfully. id = $id";
				}else{
					$this->messages  = "add failed. sql : $sql . <br>";
					$this->messages .= "error: " .  mysql_error($this->conn);
				}
			}catch(Exception $e){
				$this->messages = $e->getMessage();
				
				$id	= -1;
			}
		}else{
			$id	= -1;
			
			$this->message = 'input object is not instance of KeywordDomain.';
		}
			
		return $id;
	}
	
	public function increase($keyword)
	{
		$sql 	 = "INSERT INTO search_keywords SET keywords = '";
		$sql	.= addslashes($keyword);
		
		$sql 	.= "', searches = 1, createdTime = UTC_TIMESTAMP() ON DUPLICATE KEY UPDATE searches = searches + 1";
		
		try{
			$rs  	= mysql_query($sql, $this->conn);
			if($rs){
				return 1;
			}else{
				$this->messages  = "add failed. sql : $sql . <br>";
				$this->messages .= "error: " .  mysql_error($this->conn);
			}
		}catch(Exception $e){
			$this->messages = $e->getMessage();
		}
		
			
		return -1;
	}
	
	/* delete news class
	 *
	 * @param  int    	id
	 * @param  string  	status
	 * @param  return 	boolean
	 */
	public function delete($id)
	{
		$sql = "DELETE FROM vote WHERE id = $id";
		
		$rs	 = mysql_query($sql, $this->conn) or die (mysql_error());
		if($rs)
			return true;
		else
			return false;
	}
	
	public function increaseWhite($couponId)
	{
		$sql 	 = "INSERT INTO vote SET white = 1";

		$sql 	.= ", couponId = ";
		$sql	.= $couponId;
		
		$sql	.= " ON DUPLICATE KEY UPDATE white = white + 1";
		
		try{
			$rs  	= mysql_query($sql, $this->conn);
			if($rs){
				$this->messages = "add successfully.";
			}else{
				$this->messages  = "add failed. sql : $sql . <br>";
				$this->messages .= "error: " .  mysql_error($this->conn);
			}
		}catch(Exception $e){
			$this->messages = $e->getMessage();
			
			$id	= -1;
		}
	}
	
	public function increaseBlack($couponId)
	{
		$sql 	 = "INSERT INTO vote SET black = 1";

		$sql 	.= ", couponId = ";
		$sql	.= $couponId;
		
		$sql	.= " ON DUPLICATE KEY UPDATE black = black + 1";
		
		try{
			$rs  	= mysql_query($sql, $this->conn);
			if($rs){
				$this->messages = "add successfully.";
			}else{
				$this->messages  = "add failed. sql : $sql . <br>";
				$this->messages .= "error: " .  mysql_error($this->conn);
			}
		}catch(Exception $e){
			$this->messages = $e->getMessage();
			
			$id	= -1;
		}
	}
	
	public function getKeywords($page = 0, $discount = 20)
	{
		return $this->getCustoms('', '', $page, $discount);
	}
	
	public function getCouponKeywordId($keywords)
	{
		$returnData = null;
		
		$sql  = 'SELECT id FROM keywords_coupon WHERE keywords = \'';
		$sql .= addslashes($keywords);
		$sql .= "'";

		try {
			$rs = mysql_query($sql, $this->conn);
			if ($row = mysql_fetch_row($rs)) {
				$returnData = $row[0];
			} else {
				$this->messages = "get data failed by Id. sql : $sql <br>";
				$this->messages .= "error: " . mysql_error($this->conn);

				return null;
			}
		} catch (Exception $e) {
			$this->messages = $e->getMessage();

			return null;
		}
		
		return $returnData;
	}
	
	/* list
	 *
	 * @ param  string 	class id
	 * @ param  string 	page
	 * @ param  string 	discount
	 * @ param  string 	url
	 * @ param  return 	array
	 */
	public function getCustoms($where, $order, $page = 0, $discount = 20)
	{
		$arrback = array();
		
		try{
			$sql = "SELECT count(1) FROM vote $where";
	
			$rs  = mysql_query($sql, $this->conn);
			
			list($total) = mysql_fetch_row($rs);
			
			$this->mutilPager($total, $page, $discount);
			
			if(!empty($total)){
				$sql  = 'SELECT couponId, white, black';
				$sql .= " FROM keywords_coupon  $where $order";
				$sql .= " LIMIT $this->firstcount, $discount";
				
				$rs  = mysql_query($sql, $this->conn);
				if($rs){
					while($row = mysql_fetch_array($rs)){
						$oKeywordDomain = new KeywordDomain();

						$oKeywordDomain->setCouponId($row[0]);
						$oKeywordDomain->setWhite($row[1]);
						$oKeywordDomain->setBlack($row[2]);
						
						$arrback[] = $oKeywordDomain;
					}
				}else {
					$this->message = "select failed. sql : $sql";
					$this->message .= "<br>error: " . mysql_error($this->conn);

				}
			}
		}catch(Exception $e){
			$this->message = $e->getMessage();
			
			return null;
		}
		
		return $arrback;
	}
	
	
	
	public function getMessage() 
	{
		return $this->message;
	}

	
	
}
?>
